package com.ztewatch.wearcontacts.presenter;

import java.util.ArrayList;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog.Calls;

import com.ztewatch.wearcontacts.calllog.CallLogQuery;
import com.ztewatch.wearcontacts.view.ILoadCallLogDataView;

public class LoadCallLogDataPresenter {
	private Activity mActivity;
	private ILoadCallLogDataView mCallLogDataView;
	public LoadCallLogDataPresenter(Activity activity, ILoadCallLogDataView callLogDataView){
		mActivity = activity;
		mCallLogDataView = callLogDataView;
	}
	
	private final LoaderCallbacks<Cursor> callLogLoaderCallbacks = new LoaderCallbacks<Cursor>() {

		@Override
		public void onLoaderReset(Loader<Cursor> arg0) {

		}

		@Override
		public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
			mCallLogDataView.getCallLogLoadFinishCursor(c);
		}

		@Override
		public Loader<Cursor> onCreateLoader(int id, Bundle args) {
			CursorLoader cursorLoader = new CursorLoader(mActivity);
			configCursorLoader(cursorLoader, Calls.MISSED_TYPE, true, -1);
			return cursorLoader;
		}

		private void configCursorLoader(CursorLoader cursorLoader,
				int callType, boolean newOnly, long newerThan) {
			Uri uri = Calls.CONTENT_URI
					.buildUpon()
					.appendQueryParameter(Calls.LIMIT_PARAM_KEY,
							Integer.toString(1000)).build();

			StringBuilder where = new StringBuilder();
			ArrayList<String> selectionArgs = new ArrayList<String>();

			if (newOnly) {
				where.append(Calls.NEW);
				where.append(" = 1");
			}

			if (callType > -1) {
				if (where.length() > 0) {
					where.append(" AND ");
				}
				// Add a clause to fetch only items of type voicemail.
				where.append(String.format("(%s = ?)", Calls.TYPE));
				// Add a clause to fetch only items newer than the requested
				// date
				selectionArgs.add(Integer.toString(callType));
			}

			if (newerThan > 0) {
				if (where.length() > 0) {
					where.append(" AND ");
				}
				where.append(String.format("(%s > ?)", Calls.DATE));
				selectionArgs.add(Long.toString(newerThan));
			}

			final String selection = where.length() > 0 ? where.toString()
					: null;
			cursorLoader.setUri(uri);
			cursorLoader.setSelection(selection);
			cursorLoader.setSelectionArgs(selectionArgs.toArray(new String[0]));
			cursorLoader.setProjection(CallLogQuery._PROJECTION);
			cursorLoader.setSortOrder(Calls.DEFAULT_SORT_ORDER);
		}
	};
	
	public void initLoader(int arg0){
		mActivity.getLoaderManager().initLoader(arg0, null, callLogLoaderCallbacks);
	}
	
	public void restartLoader(int arg0) {
		mActivity.getLoaderManager().restartLoader(arg0, null, callLogLoaderCallbacks);
	}

	public void destroyLoader(int arg0) {
		mActivity.getLoaderManager().destroyLoader(arg0);
	}
}
